<?php
/**
 * Created by PhpStorm.
 * User: hebidu
 * Date: 15/7/2
 * Time: 13:46
 */

namespace Admin\Controller;


use Admin\Api\AngelRoleApi;
use Admin\Api\DatatreeApi;
use Admin\Api\MemberApi;
use Admin\Api\NewmemberApi;
use Admin\Api\PartnerRoleApi;
use Shop\Api\WalletApplyApi;
use Shop\Api\WithdrawApi;
use Common\Api\WeixinApi;
use Weixin\Api\WxaccountApi;
use Shop\Api\WalletHisApi;
use Admin\Api\MessageApi;
use Admin\Model\MessageModel;

class ApplyMoneyController extends AdminController
{

    protected $wxapi;
    protected function _initialize(){
        parent::_initialize();
        $result = apiCall(WxaccountApi::GET_INFO, array( array('id' => 6)));
        if ($result['status'] && is_array($result['info'])) {
            $this -> wxaccount = $result['info'];
            $this -> wxapi = new WeixinApi($this -> wxaccount['appid'], $this -> wxaccount['appsecret']);
        } else {
            exit("公众号信息获取失败，请重试！");
        }
    }


    /*
     * 提现列表
     * */
    public function index(){
        if(empty(I('nickname'))){
            $map = null;
        }else{
            $map['nickname'] = I('nickname');
        }
        $page = array('curpage' => I('get.p', 0), 'size' => C('LIST_ROWS'));
        $order = " create_time desc ";

        $applyinfo = apiCall(WithdrawApi::WXUERQUERY,array($map,$page,$order,$map,'itboye_withdraw.id,itboye_wxuser.nickname,itboye_wxuser.openid,itboye_withdraw.money,itboye_withdraw.account_name,itboye_withdraw.create_time,itboye_withdraw.status,itboye_withdraw.cash_account,itboye_withdraw.reason'));//采用连表查询

        if($applyinfo['status']){
            $this->assign('nickname',$map['nickname']);
            $this->assign('show',$applyinfo['info']['show']);
            $this->assign('list',$applyinfo['info']['list']);
            $this->display();
        }else{

            $this->error(L('UNKNOWN_ERR'));
        }
    }


    /*
     * 审核异步提交
     * */
    public function ajax_reviewed(){
        //true代表审核通过
        //false代表审核失败
        $map['id'] = I('id');
        if(I('true_false') == '1'){
            $save['status'] = 1;
            $reviewedres = apiCall(WithdrawApi::SAVE,array($map,$save));
            $verifyres = "恭喜您的提现审核通过了";
        }elseif(I('true_false') == '2'){
            $save['status'] = 2;
            $save['reason'] = I('reason');
            $reviewedres = apiCall(WithdrawApi::SAVE,array($map,$save));
            $verifyres = I('reason');
        }


        if($reviewedres['status']){
            $withres = apiCall(WithdrawApi::GET_INFO,array($map));
            if($withres['status']){
                $spenddata['touser'] = I('openid');
                $spenddata['template_id'] = "wZWEIAzLTV-rudrWOahjb6w5VcMpeYBqutScsaKqDMM";//信息模板
                $spenddata['url'] = "http://weixin.qq.com/download";
                $spenddata['data']['first']['value'] = '您的提现申请已处理';

                $spenddata['data']['keyword1']['value'] = $withres['info']['money'];

                $spenddata['data']['keyword2']['value'] = $withres['info']['bank_branch'];

                $spenddata['data']['keyword3']['value'] = date('Y-m-d',$withres['info']['create_time']);

                $spenddata['data']['keyword5']['value'] = date('Y-m-d',time());

                if(I('true_false') == '1'){
                    $spenddata['data']['keyword4']['value'] = "审核通过";
                }else{
                    $spenddata['data']['keyword4']['value'] = "审核不通过";
                }
               $spenddata['data']['value'] = $verifyres;

                $spendres =  $this -> wxapi-> sendTmplMsgToFans($spenddata);
                if($spendres['status']){
                    $this->success('消息发送成功');
                }else{
                    $this->success('消息发送失败');
                }
            }

        }



    }
}